package cn.scut.user.controller;

import cn.scut.user.pojo.User;
import cn.scut.user.service.UserService;
import cn.scut.user.utils.Model;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    UserService userService;

    @GetMapping("/login")
    public Model apiLogin(@RequestBody JSONObject jsonObject) {
        String openid = jsonObject.getString("openid");
        String username = jsonObject.getString("username");
        User user = userService.userRegisterAndLogin(openid, username);
        return new Model(true, user, "用户登录成功");
    }

    @PutMapping("/upgrade")
    public Model apiUpgrade(@RequestBody JSONObject jsonObject) {
        Integer experience = jsonObject.getInteger("experience");
        String openid = jsonObject.getString("openid");
        User user = userService.userUpgrade(openid, experience);
        return new Model(true, user, "经验增加成功");
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
